﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Library.DbObjects.Factory;
using Stimulsoft.Report;
using System.Xml;
using System.IO;

namespace NameplateReport
{
    public partial class SearchNamePlate : UserControl
    {
        public INamePlateManager INamePlateManager { get; set; }
        BaseObjectFactory Instance = new BaseObjectFactory();
        private BindingSource bindingSource1 = new BindingSource();

        public SearchNamePlate()
        {
            InitializeComponent();
        }

        private void SearchNamePlate_Load(object sender, EventArgs e)
        {
            SearchNamePlates(txtCode.Text);
        }
        private void SearchNamePlates(string keyword)
        {
            dgList.AutoGenerateColumns = false;
            if (!string.IsNullOrWhiteSpace(keyword))
                Instance.Query.SqlBuilder = string.Format("select * from NamePlates where code like '%{0}%'", keyword);
            else
                Instance.Query.SqlBuilder = string.Format("select * from NamePlates ");

            var list = Instance.Query.ExecuteForObjects("NamePlates");
            bindingSource1.Clear();
            foreach (var item in list)
            {
                bindingSource1.Add(item);
            }

            dgList.DataSource = bindingSource1;
            dgList.Refresh();
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchNamePlates(txtCode.Text);
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (INamePlateManager != null)
            {
                INamePlateManager.Add();
            }
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            if (INamePlateManager == null) return;
            string code = string.Empty;
            if (dgList.SelectedRows.Count == 1)
            {
                code += dgList.SelectedRows[0].Cells[0].Value;
            }
            else
            {
                if (dgList.SelectedCells.Count == 1)
                {
                    code += dgList.Rows[dgList.SelectedCells[0].RowIndex].Cells[0].Value;
                }
            }
            if (!string.IsNullOrEmpty(code))
            {
                Instance.Query.SqlBuilder = string.Format("select * from NamePlates where code ='{0}'", code);
                var result = Instance.Query.ExecuteForObject();
                INamePlateManager.Edit(result);
            }
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchNamePlates(txtCode.Text);

        }

        private void cmdPreview_Click(object sender, EventArgs e)
        {
            StiReport report = new StiReport();
            
            //report.RegData("Demo", (object)null);       
            var path = string.Format(@"{0}\{1}", Environment.CurrentDirectory, @"Reports\Nameplate.mrt");
            var template = File.ReadAllText(path);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(template);
            var files = xmlDoc.GetElementsByTagName("File");
            foreach (XmlNode file in files)
            {
                var indexPath = file.InnerText.LastIndexOf('\\');
                if (indexPath > 0)
                {
                    file.InnerText = string.Format(@"{0}\{1}", Environment.CurrentDirectory, file.InnerText.Substring(indexPath + 1));
                }
            }            
            using (var ms = new MemoryStream())
            {
                xmlDoc.Save(ms);
                ms.Seek(0, SeekOrigin.Begin);
                report.Load(ms);
                report.Show();
            }
        }
    }
}
